Ext.define('TS.view.inv.ProductBatchNoDialog', {
    extend: 'Ext.window.Window',
    alias : 'widget.prodbatchnodialog',
	mixins: {
        observable: 'Ext.util.Observable'
    },
    constructor: function () {
    	this.callParent();
        this.addEvents(
        {'selected':true}
        );
    },
    title : '商品批号查询',
    width:600,
    layout:'fit',
    height:400,
    config: {
    	prodId:"",//设置商品ID
    	isOneShow:true,//只有一条记录是否显示窗口
    	precord:''
    },
    modal:true,
    initComponent: function() {
    	Ext.apply(this, {
    		tbar :[
				{
					xtype: 'triggerfield',
					labelWidth :50,
					fieldLabel:'查询条件:',
					width:400,
					margin:'0,0,10,10',
	    	    	emptyText:'请输入商品编码或名称',
	    	    	trigger1Cls:Ext.baseCSSPrefix + "form-search-trigger",
	    	    	onTriggerClick:function(e){
	    	    		e.keyCode=e.ENTER;//设置为Enter事件
	    	    		this.fireEvent('specialkey',this,e);//点客户查询图标执行搜索
	    	    	},
	    	    	listeners: {
						specialkey:{
							fn:function(field,e){
								//回车搜索商品
								if(e.keyCode==e.ENTER){
									var txt=field.value;
									//Ext.Msg.alert('提示',"field是否为空?"+Ext.isEmpty(field)+"<br>Ext.isEmpty('',true)的值:"+Ext.isEmpty(txt,true)+"<br>Ext.isEmpty('',false)的值"+Ext.isEmpty(txt,false));
									if(!Ext.isEmpty(txt)){
										var view=Ext.create('TS.view.base.ProductSearchDialog');
								    	view.setVal(txt);
								    	view.on({
										    selected: function(records){
										    	if(records){
										    		if(records.length>0){
										    			var pid=records[0].get('productId');
										    			var win=this.up('window');
										    			win.setProdId(pid);
										    			var form=win.down('form');
										    			form.loadRecord(records[0])
										    			var grid=win.down('productbatchnolist')//items.items[0].items.items[0];
										    			grid.store.load({
															params:{prodId:pid},
												    		callback:function(records, operation, success){
												    			
												    		},
													   		scope: this
														});
										    		}
										    	}
										    	view.close();
										    },
										    scope: this
										});
								    	view.show();
									}
								}
							}
						}
					}
				}
    		],
    		items:[{
    		    xtype: 'panel',
                layout: 'border',
                items:[
	        		Ext.create('TS.view.inv.ProductBatchNoList',{
	        			autoScroll: true,
	        			title:'',
	        			closable: false,
	        			region:'center',
	        			listeners: {
	        				//双击
							itemdblclick:{
								fn:function(grid, record, item, index, e, eOpts){
									var records=[];
									records.push(record);
									var win=this.up('window');
									win.fireEvent('selected',records);
		                			win.hide();
								}
							},
							//回车
							cellkeydown:{
								fn:function(grid, td, cellIndex, record, tr, rowIndex, e, eOpts ){
									if(e.keyCode==e.ENTER){
										var records=[];
										records.push(record);
										var win=this.up('window');
										win.fireEvent('selected',records);
			                			win.hide();
									}
								}
							}
						}        			
	        		}),
	        		{
	        			xtype: 'form',
	        			region:'south',
	        			collapsible :true,
		                defaultType:'fieldcontainer',
					    defaults:{
								layout: {
									type:'hbox',//横排布局
									align:'stretch'
								},
								fieldDefaults:{
									readOnly:true,  
									labelAlign:'left'
						    	},
						    	defaultType:'textfield'
						    	
						},
						items:[
							{
								items:[
									{
				                    	name : 'productName',
				                        fieldLabel: '商品名称',
				                        width:465
				                    }
								]
							},
							{
								items:[
									{
				                        name : 'productCode',
				                        fieldLabel: '商品编号'
				                    },
				                    {
				                    	name : 'packSpec',
				                        fieldLabel: '规格'
				                    }
								]
							},
							{
								items:[
									{
				                    	name : 'manuf',
				                        fieldLabel: '生产企业'
				                    },
				                    {
				                    	name : 'unit',
				                        fieldLabel: '单位'
				                    }
								]
							}
						]
	        		}
    			]
    		}],
        	buttons:[
	        	{
	                text: '确认',
	                scope: this,
	                handler:function(){
	                	var grid=this.down('productbatchnolist');//this.items.items[0].items.items[0];
	                	var records=grid.getSelectionModel().getSelection();
	                	//确认没有选择行则不执行.
	                	if(records.length>0){
	                		this.fireEvent('selected',records);
	                		this.hide();
	                	}
	                	
	                }
	            },
	            {
	                text: '取消',
	                scope: this,
	                handler: this.close
	            }
	        ]
    	});

        this.callParent(arguments);
    },
	listeners: {
		render:{
			fn:function(){
				//var grid=this.items.items[0].items.items[0];
				var grid=this.down('productbatchnolist');
				var pid=this.getProdId();
				
				if(!Ext.isEmpty(pid)){
					grid.store.load({
						params:{prodId:pid},
			    		callback:function(records, operation, success){
			    			if(records){
			    				//如果只有一条记录并且只有IsOneShow为false.
				    			if(records.length==1 && !this.getIsOneShow()){
				    				this.fireEvent('selected',records);
			                		this.hide();
				    			}
				    			if(records.length>0){
				    				//多条记录默认选择第一条
				    				grid.getSelectionModel().select(records[0],true);
									
				    			}
			    			}
			    		},
				   		scope: this
					});					
				}else{
					grid.store.removeAll();
				}
			}
		},
		show:{
			fn:function(){
				var grid=this.down('productbatchnolist');//this.items.items[0].items.items[0];
				//如有数据默认选择第一行并设置焦点为Grid,支持上下键操作和回车,没有数据焦点停在条件输入框
				var product=this.getPrecord();
				if(!Ext.isEmpty(product)){
					var form=this.down('form');
					form.loadRecord(product)
				}
				
				if(grid.store.getTotalCount()>0){
					grid.focus(100);
				}else{
					var txtfield=this.down('triggerfield');
					txtfield.focus('',100);
				}
				
			}
		}
	}
});